package com.by.service.impl;

import com.by.bean.Enquiry;
import com.by.common.bean.PageBean;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.utils.Assert;
import com.by.mapper.EnquiryDetailMapper;
import com.by.mapper.EnquiryMapper;
import com.by.query.SaleEnquiryQuery;
import com.by.service.EnquiryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class EnquiryServiceImpl extends BaseServiceImpl<Enquiry, EnquiryMapper> implements EnquiryService {

    @Autowired
    EnquiryDetailMapper enquiryDetailMapper;

    @Transactional
public int save(Enquiry entity){
//保存询价单
        int rs=super.save(entity);
        Assert.isNotSuccess(rs,"保存询价单失败");
//保存明细
        if (null!=entity.getEnquiryDetails()&&entity.getEnquiryDetails().size()>0){
            //批量保存
rs=enquiryDetailMapper.bachSave(entity.getId(),entity.getEnquiryDetails());
Assert.isNotSuccess(rs,"保存询价明细失败");
        }
        return 1;
}

    @Override
    public PageBean queryEnquiry(SaleEnquiryQuery query) {
        PageHelper.startPage(query.getPage(),query.getPageSize());
        List list=baseMapper.queryEnquiry(query);
        PageInfo info=new PageInfo<>(list);
        return new PageBean(query.getPage(),query.getPageSize(),info.getTotal(),list);
     }
}
